/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.feriaderemates.dao.listing;

import com.feriaderemates.domain.message.Message;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Service;

/**
 *
 * @author Lenovo
 */

@Slf4j
@Service("messagingDao")
public class MessagingDao extends JdbcDaoSupport implements MessagingDaoInterface{
    private static final MessagingDao msgDao = new MessagingDao();
    
    //Queries strings
    private static String INSERT_MESSAGE = "INSERT INTO portal.user_message (sender_username,receiver_username,message,subject,date) "
            + " VALUES (?,?,?,?,NOW())";
    private static String GET_MSG_BY_ID = "SELECT * FROM portal.user_message where user_message_id = ?";
    private static String GET_MSGLIST_BY_RECEIVER = "SELECT * FROM portal.user_message where receiver_username = ?";
    private static String GET_MSGLIST_BY_SENDER = "SELECT * FROM portal.user_message where sender_username = ?";
    /**
     * Adds a Message sent to a user. The fields to add are:
     * senderUsername, receiverUsername, message and subject
     *
     * @param message the Message to add
     * @return the Message's identifier (auto-generated message key)
     */
    public long addMessage( final Message message) {
         KeyHolder keyHolder = new GeneratedKeyHolder();
       
        getJdbcTemplate().update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(INSERT_MESSAGE, Statement.RETURN_GENERATED_KEYS);
                ps.setString(1, message.getSenderUsername());
                 ps.setString(2, message.getReceiverUsername());
                 ps.setString(3, message.getMessage());
                  ps.setString(4, message.getSubject());
                return ps;
            }
        }, keyHolder);
        Object key = keyHolder.getKeys().get("user_message_id");
        return (Long) key;
    }

    public Message getMessageById(long messageId) {
        return (Message) getJdbcTemplate().queryForObject(GET_MSG_BY_ID,new Object[]{messageId}, new MessagingRowMapper());
    }

    public List<Message> getReceivedMessages(String receiverUsername) {
        return getJdbcTemplate().query(GET_MSGLIST_BY_RECEIVER, new Object[]{receiverUsername},new MessagingRowMapper());
    }

    public List<Message> getSentMessages(String senderUsername) {
        return getJdbcTemplate().query(GET_MSGLIST_BY_SENDER, new Object[]{senderUsername},new MessagingRowMapper());
    }
    
}
